package com.eyu.activity_test;

import android.R.raw;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	private Context context;
	private LayoutInflater inflater;

	public MyExpandableListAdapter(Context ctx) {
		this.context = ctx;
		inflater = LayoutInflater.from(context);
	}
	


	// 设置组视图的图片
	int[] logos = new int[] { R.drawable.wei, R.drawable.shu, R.drawable.wu };
	// 设置组视图的显示文字
	private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };
	// 子视图显示文字
	private String[][] generals = new String[][] {
			{ "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },
			{ "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },
			{ "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }

	};
	// 子视图图片
	public int[][] generallogos = new int[][] {
			{ R.drawable.xiahoudun, R.drawable.zhenji, R.drawable.xuchu,
					R.drawable.guojia, R.drawable.simayi, R.drawable.yangxiu },
			{ R.drawable.machao, R.drawable.zhangfei, R.drawable.liubei,
					R.drawable.zhugeliang, R.drawable.huangyueying,
					R.drawable.zhaoyun },
			{ R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
					R.drawable.zhouyu, R.drawable.sunshangxiang } };

	// 自己定义一个获得文字信息的方法
	TextView getTextView() {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 64);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL);
		textView.setPadding(36, 0, 0, 0);
		textView.setTextSize(20);
		textView.setTextColor(Color.BLACK);
		return textView;
	}

	//重写ExpandableListAdapter中的各个方法
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return generalsTypes.length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return generalsTypes[groupPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return generals[groupPosition].length;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return generals[groupPosition][childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		convertView = getConvertViewByInflater(groupPosition);
		return convertView;
		
//		LinearLayout ll = new LinearLayout(
//				context);
//		ll.setOrientation(0);
//		ImageView logo = new ImageView(context);
//		logo.setImageResource(logos[groupPosition]);
//		logo.setPadding(50, 0, 0, 0);
//		ll.addView(logo);
//		TextView textView = getTextView();
//		textView.setTextColor(Color.BLACK);
//		textView.setText(getGroup(groupPosition).toString());
//		ll.addView(textView);
//
//		return ll;
	}
	
	private View getConvertViewByInflater(int groupPosition) {
		View view = new View(context);
		view = inflater.inflate( R.layout.group_items, null);
		GroupViewHolder holder = new GroupViewHolder();
		holder.logo = (ImageView) view.findViewById(R.id.img_logo);
		holder.name = (TextView) view.findViewById(R.id.txv_name);
		
		view.setTag(holder);
		
		holder.logo.setImageResource(logos[groupPosition]);
		holder.name.setTextColor(Color.BLACK);
		holder.name.setText(getGroup(groupPosition).toString());
		
		return view;
	}
	
	class GroupViewHolder{
		ImageView logo;
		TextView name;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LinearLayout ll = new LinearLayout(
				context);
		ll.setOrientation(0);
		ImageView generallogo = new ImageView(
				context);
		generallogo
				.setImageResource(generallogos[groupPosition][childPosition]);
		ll.addView(generallogo);
		TextView textView = getTextView();
		textView.setText(getChild(groupPosition, childPosition)
				.toString());
		ll.addView(textView);
		return ll;
	}

	@Override
	public boolean isChildSelectable(int groupPosition,
			int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}
